<template>
  <div class="app-container">
    <!-- 搜索表单 -->
    <el-card class="box-card">
      <el-form :model="searchForm" :inline="true" size="small" label-width="90px">
        <el-form-item label="请求地址">
          <el-input v-model="searchForm.logUrl" clearable placeholder="请求地址" />
        </el-form-item>
        <el-form-item label="Controller">
          <el-input v-model="searchForm.logController" clearable placeholder="控制层" />
        </el-form-item>
        <el-form-item label="请求IP">
          <el-input v-model="searchForm.logIp" clearable placeholder="ip" />
        </el-form-item>
        <el-form-item label="请求地址">
          <el-input v-model="searchForm.logIpAddress" clearable placeholder="logIpAddress" />
        </el-form-item>
        <el-form-item label="日志状态">
          <el-select
            v-model="searchForm.logStatus"
            placeholder="选择状态"
            clearable
            size="small"
            style="width: 150px"
          >
            <el-option
              v-for="dict in logOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="日志类型">
          <el-select
            v-model="searchForm.logType"
            placeholder="选择状态"
            clearable
            size="small"
            style="width: 150px"
          >
            <el-option
              v-for="dict in typeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="创建时间">
          <el-date-picker
            v-model="dateTime"
            type="daterange"
            range-separator="-"
            style="width: 240px"
            value-format="yyyy-MM-dd"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="search">查询</el-button>
          <el-button type="warning" icon="el-icon-refresh" @click="searchForm = { pageNum: 1,pageSize: 10,params: {}};getByPage()">
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 搜索表单结束 -->
    <!-- 操作按钮组 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="toDelete"
        >删除
        </el-button>
      </el-col>
    </el-row>
    <!-- 操作按钮组结束 -->
    <!-- 数据表格开始 -->
    <div class="data-table">
      <el-table
        header-row-class-name="pochi-table-header"
        :data="dataPage.rows"
        stripe
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" class="demo-table-expand">
              <el-form-item label="请求地址">
                <div :show-overflow-tooltip="true">{{ props.row.logUrl }}</div>
              </el-form-item>
              <el-form-item label="浏览器UA标识">
                <span :show-overflow-tooltip="true">{{ props.row.logUa }}</span>
              </el-form-item>
              <el-form-item label="控制层">
                <span :show-overflow-tooltip="true">{{ props.row.logController }}</span>
              </el-form-item>
              <el-form-item label="响应时间">
                <span :show-overflow-tooltip="true">{{ props.row.logTime }}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column type="selection" align="center" />
        <el-table-column prop="logMethod" label="请求方式" align="center" />
        <el-table-column prop="logStatus" label="状态" align="center" :formatter="logFormat" />
        <el-table-column prop="logIp" label="IP" align="center" />
        <el-table-column prop="logIpAddress" label="IP地址" align="center" />
        <el-table-column prop="createdDate" label="创建时间" align="center" />
        <el-table-column prop="createdBy" label="创建人" align="center" />
        <el-table-column label="操作" width="150px" align="center">
          <template slot-scope="{row}">
            <el-button type="text" icon="el-icon-document" @click="toInfo(row.logId)">详情</el-button>
            <el-button type="text" icon="el-icon-delete" @click="toDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 数据表格结束 -->
    <!-- 分页组件开始 -->
    <div class="pageable">
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="searchForm.pageNum"
        :limit.sync="searchForm.pageSize"
        @pagination="getByPage"
      />
    </div>
    <!-- 分页组件结束 -->
    <!-- 详情弹窗 -->
    <el-dialog
      title="日志详情"
      :visible.sync="infoDialog"
      width="40%"
    >
      <sys-log-info :active-id="activeId" />
    </el-dialog>

    <!-- 详情弹窗结束 -->
  </div>
</template>

<script>
import sysLogApi from '@/api/system/sys-sysLog'
import sysLogInfo from './sys-log-info'
import { hideFullScreenLoading, showFullScreenLoading } from '@/utils/loadIng'

export default {
  components: {
    sysLogInfo
  },
  data() {
    return {
      // 查询表单
      searchForm: {
        // 当前页
        pageNum: 1,
        // 每页条数
        pageSize: 10,
        params: {}
      },
      // 数据分页对象
      dataPage: {},
      // 总记录数
      total: 0,
      // 当前点击的日志ID
      activeId: '',
      // 控制详情页是否展示
      infoDialog: false,
      // 时间存储
      dateTime: undefined,
      // 选中的数组
      ids: '',
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 字典数据
      logOptions: [],
      typeOptions: []

    }
  },
  created() {
    this.getByPage()
    this.getDicts('sys_log_status').then(response => {
      this.logOptions = response.data
    })
    this.getDicts('sys_log_type').then(response => {
      this.typeOptions = response.data
    })
  },
  methods: {
    // 1 有效 0 无效字典翻译
    logFormat(row, column) {
      return this.selectDictLabel(this.logOptions, row.logStatus)
    },
    typeFormat(row, column) {
      return this.selectDictLabel(this.typeOptions, row.logType)
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.logId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    // 分页查询
    getByPage() {
      showFullScreenLoading('.data-table')
      sysLogApi.getByPage(this.addDateRange(this.searchForm, this.dateTime)).then(res => {
        this.dataPage = res
        this.total = res.total
      })
      hideFullScreenLoading()
    },
    // 搜索
    search() {
      this.searchForm.pageNum = 1
      this.getByPage()
    },
    // 显示日志详情
    toInfo(id) {
      this.activeId = id
      this.infoDialog = true
    },
    // 删除日志
    toDelete(row) {
      const ids = row.logId || this.ids
      console.log(this.ids)
      this.$confirm('是否删除该日志?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(() => {
        sysLogApi.deleteById(ids).then(res => {
          this.$message.success(res.msg)
          this.getByPage()
        })
      })
    }
  }
}
</script>

<style>
</style>
